const data = [
    {name:'张三',region:'北京'},
    {name:'c',region:'北京'},
    {name:'a',region:'上海'},
    {name:'d',region:'上海'},
    {name:'b',region:'武汉'},
    {name:'c',region:'日本'},
    {name:'c',region:'日本'},
    {name:'c',region:'日本'},
    {name:'r',region:'美国'},
]


/**
 * 先拿到所有地域名
 * 根据所有地域名获取不同数组
 * @param data
 * @returns {*[]}
 */
const getRegionList = (data)=>{
    var regionList = []; //所有地域名的数组
    regionList.push(data[0].region) // 空数组无法使用some方法
    for (let i = 1; i < data.length; i++) {
        let temp = regionList.some(item=>{
            return item === data[i].region;
        })
        if(!temp){
            regionList.push(data[i].region);
        }
    }

//console.log(regionList); //拿到所有地域名
//     console.log('=================================')

    var totalList = [];//最后筛选完后存放的数组
    var temp  = 0; //优化二次循环的循环次数
    for (let i = 0; i < regionList.length; i++) {
        var tempRegionList = [];
        for (let j = temp; j < data.length; j++) {
            if(data[j].region === regionList[i]){
                tempRegionList.push(data[j])
                //最后一个地域名走不到else 要手动放入totalList
                if(j === data.length-1){
                    totalList.push(tempRegionList);
                }
            }else{
                temp = j;
                totalList.push(tempRegionList);
                break;
            }
        }
    }
    // console.log(totalList)
    return totalList;
}

const res = getRegionList(data);
console.log(res)

